//
//  MainViewController.swift
//  SwiftWB
//
//  Created by 钟清 on 17/3/24.
//  Copyright © 2017年 Zhong Qing. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        /*
         * 纯代码创建框架
        tabBar.tintColor = UIColor.orangeColor()
        addAllChildViewControllers()
        */
    }
    
    // MARK: - Life
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(true)
        /// 1,添加加号按钮
        tabBar.addSubview(compossButton)
        /// 2,按钮位置：居中
        let rect = compossButton.frame
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)
        /// 此处y值做简单处理
        compossButton.frame = CGRect(x: 2 * width, y: 2.5, width: width, height: rect.height)
        
    }
    
    /*
     * 纯代码创建框架
    func addAllChildViewControllers() {
        
        // 1, 获取JSON数据
        guard let filePath = NSBundle.mainBundle().pathForResource("MainVCSettings.json", ofType: nil) else {
            ZQLog("没有JSON数据")
            return
        }
        
        guard let data = NSData(contentsOfFile: filePath) else {
            ZQLog("加载二进制数据失败")
            return
        }
        
        do { // 获得新VC信息
            let obj = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! [[String : AnyObject]]
            for dict in obj {
                let title = dict["title"] as? String
                let vcName = dict["vcName"] as? String
                let imageName = dict["imageName"] as? String
                addChildViewController(vcName, title: title, imageName: imageName)
                
                ZQLog("加载JSON视图界面")
                
            }
        } catch
        { // 无法获得新VC信息
            addChildViewController("HomeViewController", title: "首页", imageName: "tabbar_home")
            addChildViewController("MessageViewController", title: "信息", imageName: "tabbar_message_center")
            addChildViewController("NullViewController", title: "", imageName: "")
            addChildViewController("DiscoverViewController", title: "发现", imageName: "tabbar_discover")
            addChildViewController("ProfileViewController", title: "我", imageName: "tabbar_profile")
            
            ZQLog("加载原视图界面")
            
        }
    }
    func addChildViewController(childVcName : String? , title : String? , imageName : String?) {
        // 1, 动态获取命名空间
        guard let name = NSBundle.mainBundle().infoDictionary!["CFBundleExecutable"] as? String else {
            ZQLog("获取命名空间失败")
            return
        }
        
        // 2, 根据字符串获取Class
        var cls : AnyClass? = nil
        if let vcName = childVcName {
            cls = NSClassFromString(name + "." + vcName)
        }
        
        // 3, 根据Class创建对象
        guard let typeCls = cls as? UITableViewController.Type else {
            ZQLog("不能当做UITableViewController")
            return
        }
        let childController = typeCls.init()
        
        childController.title = title
        if let pictureName = imageName
        {
            childController.tabBarItem.image = UIImage(named: pictureName)
            childController.tabBarItem.selectedImage = UIImage (named: pictureName + "_highlighted")
        }
        let nav = UINavigationController(rootViewController:childController)
        addChildViewController(nav)
    }
    */
    
    // MARK: -  内部控制方法
    func compossButtonClick() {
        
        guard let composeVc = R.storyboard.compose.initialViewController() else {
            return
        }
        presentViewController(composeVc, animated: true, completion: nil)
    
    }
    // MARK: - Lazy
    lazy var compossButton : UIButton = {
        /// 创建按钮
        let btn = UIButton(imageName: "tabbar_compose_icon_add",backgroundImageName: "tabbar_compose_button")
        /// 监听点击
        btn.addTarget(self, action: #selector(MainViewController.compossButtonClick), forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
}

